package org.pearl.thread.demo.test001;

import java.util.concurrent.TimeUnit;

/**
 * Created by TD on 2021/4/13
 */
public class DaemonThreadTest001 {
    public static void main(String[] args) {
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    System.out.println(Thread.currentThread().getName() + "正在检测心跳");
                    try {
                        // 休眠3秒
                        // Thread.sleep(3_000);
                        TimeUnit.SECONDS.sleep(3);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, "检测心跳任务线程");
        // 设置为守护线程
        t.setDaemon(true);
        // 查看线程是否为守护线程
        // 打印线程ID
        long id = t.getId();
        ClassLoader contextClassLoader = t.getContextClassLoader();
        System.out.println(contextClassLoader);
        System.out.println(id);
        // 获取当前线程
        Thread thread = Thread.currentThread();
        System.out.println(t.getName() + "是否守护线程" + t.isDaemon());
        System.out.println(Thread.currentThread().getName() + "是否守护线程" + Thread.currentThread().isDaemon());
        t.start();
        try {
            Thread.sleep(20_000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "线程任务运行结束");
    }
}
